package com.example.CarManagement.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.CarManagement.common.Result;
import com.example.CarManagement.sys.entity.CarInfo;
import com.example.CarManagement.sys.entity.Drivers;
import com.example.CarManagement.sys.mapper.DriversMapper;
import com.example.CarManagement.sys.service.impl.CarInfoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 车辆信息表 前端控制器
 * </p>
 *
 * @author 徐佳彪
 * @since 2025-07-08
 */
@RestController
@RequestMapping("/sys/car")
public class CarInfoController {


    @Autowired
    private CarInfoServiceImpl carInfoService;

    @Resource
    private DriversMapper driversMapper;


    // 获取车辆信息，翻页查询
    @PreAuthorize("@se.hasPermi('CARINFO_SELECT')")
    @GetMapping("/list")
    public Result<?> getList(@RequestParam(required = false) Integer pageNo, @RequestParam(required = false) Integer pageSize, @RequestParam(required = false) String plateNumber, @RequestParam(required = false) Integer status) {

        // Handle cases where parameters are not provided
        if (pageNo == null) {
            pageNo = 1; // Default page number
        }
        if (pageSize == null) {
            pageSize = 10; // Default page size
        }

        if (plateNumber == null) {
            plateNumber = ""; // Default plate number filter
        }


        return carInfoService.getList(pageNo, pageSize, plateNumber, status);
    }

    // 根据id查询车辆信息
    @PreAuthorize("@se.hasPermi('CARINFO_UPDATE')")
    @GetMapping("/getCarById/{id}")
    public Result<?> getCarInfoById(@PathVariable("id") String id) {
        return Result.success(carInfoService.getById(id));
    }


    // 添加车辆信息
    @PreAuthorize("@se.hasPermi('CARINFO_ADD')")
    @PostMapping("/add")
    public Result<?> addCarInfo(@RequestBody CarInfo carInfo, @RequestHeader("authorization") String token) {
        return carInfoService.addCarInfo(carInfo, token);
    }

    // 修改车辆信息
    @PreAuthorize("@se.hasPermi('CARINFO_UPDATE')")
    @PutMapping("/update")
    public Result<?> updateCarInfo(@RequestBody CarInfo carInfo) {
        return Result.success(carInfoService.updateById(carInfo));
    }

    // 删除车辆信息
    @PreAuthorize("@se.hasPermi('CARINFO_DELETE')")
    @DeleteMapping("/delete/{id}")
    public Result<?> deleteCarInfo(@PathVariable("id") String id) {
        // 查询状态如果是出车中则不可以删除
        CarInfo carInfo = carInfoService.getById(id);
        if (carInfo.getStatus() == 1) {
            return Result.fail("出车中，无法删除");
        }

        // 查收是否绑定司机
        LambdaQueryWrapper<Drivers> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Drivers::getCurrentVehicleId, id);
        Drivers drivers = driversMapper.selectOne(queryWrapper);
        if (drivers != null) {
            return Result.fail("该车辆已绑定司机，无法删除");
        }


        carInfoService.removeById(id);
        return Result.success("删除成功");
    }
//审批人测试1
//    管理员测试2

    // 获取所有可用车辆选择器
    @PreAuthorize("@se.hasPermi('CARINFO_SELECT')")
    @GetMapping("/getCarSelector")
    public Result<?> getCarSelector() {
        return Result.success(carInfoService.getCarSelector());
    }
}
